import json
import logging

import pytest
import requests as R

def openfile():
    with open("data.json","r",encoding="UTF8") as O:
        return json.load(O)


class Testadd():
    uid=""
    sid=""
    #初始化测试环境，开始执行用例
    def setup_method(self):
        print("初始化测试环境，开始执行用例")

    #恢复测试环境，结束测试用例
    def teardown_method(self):
        print("恢复测试环境，结束测试用例")

    #登录
    def test_login(self):
        url="http://10.59.9.18/small/user/v1/login"
        body={"phone":"13811033965","pwd":"123456Aa"}
        r = R.post(url=url,json=body)
        print(r.json())
        logging.info("登陆成功")
        Testadd.uid=r.json()['result']['userId']
        Testadd.sid=r.json()['result']['sessionId']
        #文本 断言
        assert "成功" in r.text


    #1.新增收货地址 全部参数正确 返回添加成功
    def test_add1(self):
        try:
            url="http://10.59.9.18/small/user/verify/v1/addReceiveAddress"
            body={"realName":" 张向前","phone":"18603344504","address":"北京海淀区八维","zipCode":"101010"}
            header={"userId":str(Testadd.uid),"sessionId":str(Testadd.sid)}
            r=R.post(url=url,data=body,headers=header)
            print(r.json())
            logging.info("添加成功")
            #状态码断言
            assert r.status_code == 200
        except Exception as E:
            print(f"这里有错误>>{E}<<")


    #2.新增收货地址接口用户未登录返回请先进行登录
    def test_add2(self):
        url="http://10.59.9.18/small/user/verify/v1/addReceiveAddress"
        body={"realName":" 张向前","phone":"18603344504","address":"北京 海淀区 八维","zipCode":"101010"}
        header={"userId":str(Testadd.uid),"sessionId":str(Testadd.sid)}
        r=R.post(url=url,data=body)
        logging.info("请先登录")
        print(r.json())
        #状态码断言
        assert r.status_code == 200


    #3.新增收货地址接口真实姓名为空返回真实姓名不能为空
    #该接口返回结果为添加成功 但实际逻辑应为真实姓名不能为空 所以这是一个Bug
    def test_add3(self):
        url="http://10.59.9.18/small/user/verify/v1/addReceiveAddress"
        body={"realName":" ","phone":"18603344504","address":"北京 海淀区 八维","zipCode":"101010"}
        header={"userId":str(Testadd.uid),"sessionId":str(Testadd.sid)}
        r=R.post(url=url,data=body,headers=header)
        print(r.json())
        logging.info("真实姓名不能为空")
        #json断言
        assert r.json()['message'] == "添加成功"


    #4.新增收货地址接口手机号为空返回手机号不能为空
    def test_add4(self):
        url="http://10.59.9.18/small/user/verify/v1/addReceiveAddress"
        body={"realName":" 张向前","phone":"","address":"北京 海淀区 八维","zipCode":"101010"}
        header={"userId":str(Testadd.uid),"sessionId":str(Testadd.sid)}
        r=R.post(url=url,data=body,headers=header)
        logging.info("手机号不正确")
        print(r.json())
        #状态码断言
        assert "手机号" in r.text


    #5.新增收货地址接口手机号开头不是1返回手机号格式不正确
    def test_add5(self):
        url="http://10.59.9.18/small/user/verify/v1/addReceiveAddress"
        body={"realName":" 张向前","phone":"28603344504","address":"北京 海淀区 八维","zipCode":"101010"}
        header={"userId":str(Testadd.uid),"sessionId":str(Testadd.sid)}
        r=R.post(url=url,data=body,headers=header)
        logging.info("手机号不正确")
        print(r.json())
        #状态码断言
        assert r.status_code == 200

